<template>
  <div class="app-container">
    <!-- 轮播图区域 -->
    <div class="banner-container">
      <el-carousel
          :interval="4000"
          height="400px"
          type="card"
          arrow="always"
          indicator-position="outside"
      >
        <el-carousel-item v-for="(item, index) in bannerList" :key="index">
          <div class="banner-item" @click="handleBannerClick(item.link)">
            <img
                :src="item.imageUrl"
                class="banner-image"
                :alt="item.title || 'Banner ' + (index + 1)"
            />
            <div class="banner-overlay"></div>
            <div class="banner-content">
              <h3 class="banner-title">{{ item.title }}</h3>
              <p class="banner-desc" v-if="item.desc">{{ item.desc }}</p>
              <el-button type="primary" round class="banner-btn">立即查看</el-button>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 商品展示区域 -->
    <div class="product-section">
      <div class="section-header">
        <h2 class="section-title">
          <i class="iconfont icon-nongchanpin"></i>
          优质农产品推荐
        </h2>
        <el-divider></el-divider>
        <p class="section-subtitle">新鲜直达 · 产地直供 · 助农惠民</p>
      </div>

      <div class="product-list">
        <el-row :gutter="20">
          <el-col
              :xs="24" :sm="12" :md="8" :lg="6"
              v-for="(item, index) in productArr"
              :key="index"
          >
            <el-card
                class="product-card"
                shadow="hover"
                @click.native="viewProductDetail(item)"
                :body-style="{ padding: '0' }"
            >
              <div class="product-tag" v-if="item.tag">
                <span :class="'tag-' + item.tag.type">{{ item.tag.text }}</span>
              </div>
              <div class="image-wrapper">
                <img
                    :src="item.imageUrls || 'https://via.placeholder.com/300x200?text=No+Image'"
                    class="product-image"
                    :alt="item.name"
                >
              </div>
              <div class="product-content">
                <h3 class="product-name">{{ item.name }}</h3>
                <div class="product-rating" v-if="item.rating">
                  <el-rate
                      v-model="item.rating"
                      disabled
                      show-score
                      text-color="#ff9900"
                      score-template="{value}分"
                  />
                </div>
                <div class="price-stock">
                  <div class="price-box">
                    <span class="current-price">¥{{ item.price.toFixed(2) }}</span>
                    <span class="original-price" v-if="item.originalPrice">¥{{ item.originalPrice.toFixed(2) }}</span>
                  </div>
                  <span class="stock">{{ item.stock }}件库存</span>
                </div>
                <div class="product-actions">
<!--                  <el-button type="primary" size="small" @click.stop="addToCart(item)">加入购物车</el-button>-->
                  <el-button type="success" size="small" @click.stop="viewProductDetail(item)">查看详情</el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script setup>
import axios from "axios";
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import { useRouter } from 'vue-router';

const router = useRouter();

//定义保存商品数据的数组
const productArr = ref([]);

// 查看商品详情
const viewProductDetail = (product) => {
  router.push({
    path: '/product/detail',
    query: { id: product.id }
  });
};

// //定义保存商品信息用于添加购物车的对象
// const cartItem = ref({
//   productId: null,
//   quantity: 1,
//   productName:null
// });
// //定义获取购物车对象信息的方法
// const cartItemInfo = (productId, quantity, productName) => {
//   cartItem.value = {
//     productId: productId,
//     quantity: quantity,
//     productName: productName
//   }
// }
// // 添加到购物车
// const addToCart = (product) => {
//   // 这里可以添加购物车逻辑
//   ElMessage.success(`已添加 ${product.name} 到购物车`);
// };

//定义加载上架商品的方法
const loadProduct = () => {
  axios.get(BASE_URL+'/api/v1/product/allPutProduct').then((response)=>{
    if (response.data.code == 2000){
      productArr.value = response.data.data.map(item => ({
        ...item,
        price: Number(item.price) || 0,
        stock: Number(item.stock) || 0,
        rating: (Math.random() * 1 + 4).toFixed(1), // 模拟评分4.0-5.0
        originalPrice: Number(item.price) + (Math.random() * 20 + 10), // 模拟原价
        tag: Math.random() > 0.7 ? {
          type: Math.random() > 0.5 ? 'hot' : 'new',
          text: Math.random() > 0.5 ? '热销' : '新品'
        } : null
      }));
    }else {
      ElMessage.error(response.data.msg);
    }
  }).catch(error => {
    ElMessage.error('加载商品失败: ' + error.message);
  });
}

//已进入页面自动加载
onMounted(()=>{
  loadProduct();
})

// 轮播图数据
const bannerList = ref([
  {
    imageUrl: 'http://localhost:8080/2025/05/14/优质农产品特惠.png',
    title: '优质农产品特惠',
    desc: '精选产地直供，品质保障',
    link: '/promotion',
  },
  {
    imageUrl: 'http://localhost:8080/2025/05/14/新鲜果蔬直供.png',
    title: '新鲜果蔬直供',
    desc: '24小时从田间到餐桌',
    link: '/products/vegetables',
  },
  {
    imageUrl: 'http://localhost:8080/2025/05/14/助农扶贫专区.png',
    title: '助农扶贫专区',
    desc: '购买即助力乡村振兴',
    link: '/help-farmers',
  },
  {
    imageUrl: 'http://localhost:8080/2025/05/14/当季热销推荐.png',
    title: '当季热销推荐',
    desc: '时令鲜品，不容错过',
    link: '/hot-products',
  }
]);

// 点击轮播图跳转
const handleBannerClick = (link) => {
  if (link) {
    router.push(link);
  }
};
</script>

<style scoped lang="scss">
.app-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 轮播图样式 */
.banner-container {
  width: 100%;
  margin: 20px 0 40px;
  border-radius: 12px;
  overflow: hidden;

  :deep(.el-carousel) {
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  }

  :deep(.el-carousel__container) {
    border-radius: 12px;
  }

  :deep(.el-carousel__item) {
    border-radius: 12px;
    overflow: hidden;
  }

  .banner-item {
    position: relative;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }

  .banner-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.8s;
  }

  .banner-item:hover .banner-image {
    transform: scale(1.05);
  }

  .banner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to top, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0.3) 50%, transparent 100%);
  }

  .banner-content {
    position: absolute;
    bottom: 60px;
    left: 60px;
    color: white;
    text-align: left;
    max-width: 50%;

    .banner-title {
      font-size: 32px;
      font-weight: bold;
      margin-bottom: 15px;
      text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
    }

    .banner-desc {
      font-size: 16px;
      margin-bottom: 20px;
      line-height: 1.6;
      text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    }

    .banner-btn {
      padding: 10px 25px;
      font-size: 16px;
      font-weight: 500;
    }
  }
}

/* 商品区域样式 */
.product-section {
  margin: 40px 0 60px;

  .section-header {
    text-align: center;
    margin-bottom: 30px;

    .section-title {
      font-size: 28px;
      color: #333;
      margin-bottom: 15px;
      display: flex;
      align-items: center;
      justify-content: center;

      .iconfont {
        font-size: 32px;
        margin-right: 10px;
        color: #67C23A;
      }
    }

    .el-divider {
      width: 200px;
      margin: 0 auto 15px;
      background-color: #67C23A;
    }

    .section-subtitle {
      font-size: 16px;
      color: #909399;
    }
  }
}

/* 商品卡片样式 */
.product-list {
  margin-top: 20px;
}

.product-card {
  width: 100%;
  margin-bottom: 25px;
  border-radius: 10px;
  overflow: hidden;
  transition: all 0.3s ease;
  border: none;

  &:hover {
    transform: translateY(-8px);
    box-shadow: 0 15px 30px rgba(144, 238, 144, 0.2);
  }

  .product-tag {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 2;

    span {
      display: inline-block;
      padding: 4px 10px;
      border-radius: 4px;
      font-size: 12px;
      font-weight: bold;
      color: white;

      &.tag-hot {
        background: linear-gradient(to right, #FF6B6B, #FF8E8E);
      }

      &.tag-new {
        background: linear-gradient(to right, #4CAF50, #8BC34A);
      }
    }
  }

  .image-wrapper {
    width: 100%;
    height: 220px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;

    .product-image {
      width: 100%;
      height: 100%;
      object-fit: contain;
      transition: transform 0.5s;
    }
  }

  &:hover .product-image {
    transform: scale(1.1);
  }

  .product-content {
    padding: 15px;

    .product-name {
      font-size: 16px;
      color: #333;
      margin-bottom: 10px;
      font-weight: 500;
      height: 44px;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .product-rating {
      margin-bottom: 10px;

      :deep(.el-rate) {
        display: inline-flex;
      }
    }

    .price-stock {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 15px 0;

      .price-box {
        display: flex;
        align-items: baseline;

        .current-price {
          color: #F56C6C;
          font-size: 20px;
          font-weight: bold;
        }

        .original-price {
          color: #999;
          font-size: 14px;
          text-decoration: line-through;
          margin-left: 8px;
        }
      }

      .stock {
        color: #666;
        font-size: 14px;
      }
    }

    .product-actions {
      display: flex;
      justify-content: space-between;

      .el-button {
        flex: 1;
        margin: 0 5px;
        padding: 8px 0;
      }
    }
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .banner-container {
    margin: 15px 0 30px;
  }

  .banner-content {
    bottom: 40px !important;
    left: 40px !important;

    .banner-title {
      font-size: 28px !important;
    }
  }

  .product-section {
    margin: 30px 0 50px;
  }
}

@media (max-width: 768px) {
  .banner-container {
    margin: 10px 0 20px;

    :deep(.el-carousel) {
      height: 250px !important;
    }
  }

  .banner-content {
    bottom: 20px !important;
    left: 20px !important;
    max-width: 80% !important;

    .banner-title {
      font-size: 20px !important;
      margin-bottom: 8px !important;
    }

    .banner-desc {
      font-size: 14px !important;
      margin-bottom: 12px !important;
    }

    .banner-btn {
      padding: 8px 15px !important;
      font-size: 14px !important;
    }
  }

  .product-section {
    margin: 20px 0 40px;

    .section-header {
      .section-title {
        font-size: 22px;
      }

      .section-subtitle {
        font-size: 14px;
      }
    }
  }

  .product-card {
    .image-wrapper {
      height: 180px;
    }

    .product-content {
      padding: 12px;

      .product-name {
        font-size: 15px;
        height: 40px;
      }

      .price-stock {
        flex-direction: column;
        align-items: flex-start;

        .stock {
          margin-top: 5px;
        }
      }

      .product-actions {
        flex-direction: column;

        .el-button {
          width: 100%;
          margin: 5px 0;
        }
      }
    }
  }
}
</style>